﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.EnterpriseService;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;
using System.Data;
using Telerik.Web.UI;

namespace RN.ERP.Web.Enterprise
{
    public partial class EnterpriseStatistics : BasePage
    {
        /// <summary>
        /// 作为查询地区限定条件
        /// </summary>
        protected string AreaCodeWhere
        {
            get
            {
                return UserInfo.AreaCode;
            }
        }
        /// <summary>
        /// 保存人员类型
        /// </summary>
        private Dictionary<string, string> dict
        {
            get
            {
                if (ViewState["dict"] != null)
                {
                    return (Dictionary<string, string>)ViewState["dict"];
                }
                return GetGetDictionary();
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFlowCodes();
            }
        }

        protected void CorpStatistics_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            EnterpriseServiceClient client = new EnterpriseServiceClient();
            string flowCodes= rblFlowCodeList.SelectedValue;
            var corps = JsonConvert.DeserializeObject<StatisticsStruct>(client.GetStatisticCorps(UserInfo.CurrentArea, flowCodes));
            var data = ToDataTable(corps);
            CorpStatistics.DataSource = data;
        }

        public DataTable ToDataTable(StatisticsStruct statisticsStruct)
        {
            DataTable result = UtilitiesTool.StructToDataTable(statisticsStruct);
            if (result == null)
            {
                return result;
            }
            //显示子地区统计数据
            if (AreaCodeWhere.Length > 2)
            {
                return result.Select("areaCode='" + AreaCodeWhere + "'").CopyToDataTable();
            }          
           return result;
        }

        protected void RadAjaxManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
        {
            if (e.Argument == "ReBind")
            {
                CorpStatistics.MasterTableView.SortExpressions.Clear();
                CorpStatistics.MasterTableView.GroupByExpressions.Clear();
                CorpStatistics.Rebind();
            }
        }

        protected void CorpStatistics_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridHeaderItem)
            {
                ///隐藏地区号
                GridHeaderItem gridHearItem = e.Item as GridHeaderItem;
                gridHearItem["areaCode"].Visible = false;
            }
            int gridCount = 0;
            if (e.Item is GridDataItem)
            {
                GridDataItem gridItem = e.Item as GridDataItem;
                gridItem["areaCode"].Visible = false;
                string url = "EnterpriseCorpList.aspx";
                if (SysAreaCode == "14")
                {
                    url = "EnterpriseCorpList14.aspx";
                }
                ///根据人员类型列表取得相应列并将统计数目大于零的加链接
                foreach (var d in dict)
                {
                    if (gridItem[d.Value] != null)
                    {
                        int.TryParse(gridItem[d.Value].Text, out gridCount);
                        gridItem[d.Value].Text = "<a href='../Enterprise/" + url + "?areacode=" + gridItem["areaCode"].Text + "&classid=" + d.Key + "&flowcode=" + Server.UrlEncode(rblFlowCodeList.SelectedValue) + "&statistics=1'><span style='color: #43a9f5'>" + gridCount.ToString() + "</span></a>";
                    }
                }
            }
        }

        protected Dictionary<string, string> GetGetDictionary()
        {
            using (BasicService.BasicServiceClient client = new BasicService.BasicServiceClient())
            {
                var dictTmp = JsonConvert.DeserializeObject<Dictionary<string, string>>(client.GetSysStaticSettings("CORPCLASSID", UserInfo.CurrentArea));
                ViewState["dict"] = dictTmp;
                return dictTmp;
            }
        }


        private void BindFlowCodes()
        {
            using (BasicService.BasicServiceClient client = new BasicService.BasicServiceClient())
            {
                if (AppID == "101")
                {
                    rblFlowCodeList.Items.Clear();
                    rblFlowCodeList.Items.Add(new ListItem("已上报", "0,019|0,019,022"));
                    rblFlowCodeList.Items.Add(new ListItem("初审通过", "0,019,029|0,019,029,031|0,019,029,032"));
                    rblFlowCodeList.Items.Add(new ListItem("审核通过", "1,019,029|1,019,029,039"));
                    rblFlowCodeList.Items.Insert(0, new ListItem("全部", ""));
                }
                else
                {
                    string msg = string.Empty;
                    var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(client.GetAllStatusFlowCodesByTitle(out msg, UserInfo.RoleId, UserInfo.CurrentArea, ""));
                    rblFlowCodeList.Items.Clear();
                    foreach (var key in dic.Keys)
                    {
                        rblFlowCodeList.Items.Add(new ListItem(key, dic[key]));
                    }
                    rblFlowCodeList.Items.Insert(0, new ListItem("全部", ""));
                }
            }
        }

    }
}